<template>
    <div>
        <el-dialog v-model="isShow" :title="isAdd ? '添加菜单' : '编辑菜单'" center @close="cancel(formRef)">
            <el-form :model="form" :rules="rules" ref="formRef">
                <el-form-item prop="title" label="菜单名称" :label-width="formLabelWidth">
                    <el-input v-model="form.title" autocomplete="off" />
                </el-form-item>
                <el-form-item prop="pid" label="上级菜单" :label-width="formLabelWidth">
                    <el-select @change="changePid" v-model="form.pid" placeholder="请选择上级菜单">
                        <el-option label="顶级菜单" :value="0"></el-option>
                        <el-option v-for="item in menulist" :key="item.id" :label="item.title" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="菜单类型" :label-width="formLabelWidth">
                    <el-radio disabled v-model="form.type" :label="1">目录</el-radio>
                    <el-radio disabled v-model="form.type" :label="2">菜单</el-radio>
                </el-form-item>
                <el-form-item v-if="form.type == 1" label="菜单图标" :label-width="formLabelWidth">
                    <el-select v-model="form.icon" placeholder="请选择菜单地址">
                        <el-option v-for="item in iconlist" :key="item" :value="item">
                            <el-icon>
                                <component :is="item"></component>
                            </el-icon>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item v-else label="菜单地址" :label-width="formLabelWidth">
                    <el-select v-model="form.url" placeholder="请选择菜单地址">
                        <el-option v-for="item in indexRoutes" :key="item.path" :value="item.path"
                            :label="item.meta.title"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="2" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="cancel(formRef)">取消</el-button>
                    <el-button type="primary" @click="comfirm(formRef)">确定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup>
import { menuAdd, menuInfo, menuEdit } from '@/request/api'
import { reactive, ref, defineProps, defineEmits, defineExpose, computed } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { indexRoutes } from '@/router'
import { useStore } from 'vuex'

const store = useStore()

//定义一个变量去接收id
let newId = 0

const menulist = computed(() => store.getters['menu/getMenuList'])

const getMenu = () => {
    store.dispatch('menu/MenuListActions')
}

let formRef = ref<FormInstance>()

let form = ref<any>({
    pid: '', //上级分类编号  注意：顶级菜单 - 0 
    title: '',  //菜单名称
    icon: '',  //图标 
    type: 1,  //类型 1目录2菜单
    url: '',  //菜单地址
    status: 1  //状态   1正常2禁用
})
const rules = reactive<FormRules>({
    title: [
        { required: true, message: '请输入菜单名称', trigger: 'blur' },
        { min: 2, max: 7, message: '长度在 2 到 7 个字符', trigger: 'blur' },
    ],
    pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }],
})

const iconlist = [
    'Location',
    'Eleme',
    'Fold',
    'GoodsFilled',
    'IceCream',
    'Key',
    'Tools'
]


const formLabelWidth = '140px'

let props = defineProps({
    isShow: Boolean,
    isAdd: Boolean
})

let changePid = () => {
    form.value.type = form.value.pid == 0 ? 1 : 2;
}

// 菜单获取（一条）
let lookup = (id: number) => {
    newId = id
    menuInfo({
        id
    }).then((res: any) => {
        console.log(res, '获取一条数据');
        form.value = res.list
        form.value.id = id
    })
}
defineExpose({
    lookup
})

// 确定
const comfirm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            // console.log(form)
            if (props.isAdd) {
                // console.log('菜单添加');
                menuAdd(form.value)
                    .then((res: any) => {
                        console.log(res, '菜单添加');
                        if (res.code == 200) {
                            ElMessage({
                                type: 'success',
                                message: res.msg
                            })
                            cancel(formEl)
                            getMenu()
                        }
                    })
            } else {
                // console.log('菜单编辑');
                menuEdit({
                    ...form.value,
                    id: newId
                })
                    .then((res: any) => {
                        console.log(res, '菜单编辑');
                        if (res.code == 200) {
                            ElMessage({
                                type: 'success',
                                message: res.msg
                            })
                            cancel(formEl)
                            getMenu()
                        }
                    })
            }
        } else {
            console.log('error submit!', fields)
        }
    })
}

// 重置
let reset = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields(),
        form.value = {
            pid: '', //上级分类编号  注意：顶级菜单 - 0 
            title: '',  //菜单名称
            icon: '',  //图标 
            type: 1,  //类型 1目录2菜单
            url: '',  //菜单地址
            status: 1  //状态   1正常2禁用
        }
}

let emit = defineEmits(['cancel'])
let cancel = (formEl: FormInstance | undefined) => {
    emit('cancel')
    reset(formEl)
}


</script>

<style scoped>
</style>
